package com.atguigu.daijia.order.handle;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author 谢忠涵7
 */ //监听延迟队列
@Component
public class RedisDelayHandle {
   @Autowired
   private OrderInfoService orderInfoService;
   @Autowired
   private RedissonClient redissonClient;
   public void lister() {
       //使用线程不停的监听消息
       new Thread(()->{
           while (true) {
               RBlockingQueue<String> queueCancel = redissonClient
                       .getBlockingQueue("queue_cancel");
               try {
                   String take = queueCancel.take();
                   if(StringUtils.hasText(take)){
                       //消息不为空,就取消订单
                       orderInfoService.cancelOrder(Long.parseLong(take));
                   }
               }catch (Exception e) {
                   e.printStackTrace();
                   throw new GuiguException(ResultCodeEnum.DATA_ERROR);
               }
           }
       }).start();
   }
}
